package com.jp.knowledge.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.model.HeadLineData;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.TimeUtil;

import java.util.List;

import static com.umeng.socialize.utils.ContextUtil.getContext;

/**
 * Created by jiayen on 16/10/12.
 */

public class AttentionDetailAdpter extends JpBaseRecyViewAdapter {


    public AttentionDetailAdpter(Context context,List data) {
        super(context,data);
    }

    @Override
    public int getLayoutId() {
        return R.layout.attention_detail_item;
    }

    @Override
    public void bindViewHold(JPRecycleViewHolder holder, int position) {

        HeadLineData headLineData = (HeadLineData) data.get(position);
        TextView title = holder.getView(R.id.description);
        if(headLineData.getTitle() == null)
            title.setText("");
        else
            title.setText(headLineData.getTitle());//设置名称
        if (headLineData.isLook()) {
            title.setTextColor(mContext.getResources().getColor(R.color.gray_light));
        } else {
            title.setTextColor(mContext.getResources().getColor(R.color.font_black_think));
        }

        // 设置类型
        setTextView((TextView)holder.getView(R.id.type), headLineData.getLabel());
        // 设置作者
        setTextView((TextView)holder.getView(R.id.author), headLineData.getSource());
        // String time = TimeUtil.getStandard(headLineData.getCreatetime());
        // 创建时间
        setTextView((TextView)holder.getView(R.id.time),
                TimeUtil.friendly_time(headLineData.getCreatetime()));
        // 作者头像
        String authorIconUrl = headLineData.getIcon();
        ImageView authorIconView = holder.getView(R.id.photo);
        if(authorIconUrl == null || authorIconUrl.trim().length() == 0){
            authorIconView.setVisibility(View.GONE);
        } else {
            authorIconView.setVisibility(View.VISIBLE);
            ImageLoadUtil.loadCircle(mContext, authorIconUrl, authorIconView);
        }

        ImageView iconClose = holder.getView(R.id.icon_close);
        ImageView icon = holder.getView(R.id.desc_icon);
        ImageView icon_1 = holder.getView(R.id.desc_icon_1);
        ImageView icon_2 = holder.getView(R.id.desc_icon_2);
        ImageView icon_3 = holder.getView(R.id.desc_icon_3);
        ImageView[] imgs = new ImageView[3];
        imgs[0] = icon_1;
        imgs[1] = icon_2;
        imgs[2] = icon_3;
        List<HeadLineData.ImglistBean> list = headLineData.getImglist();
        LinearLayout muiltIcon = holder.getView(R.id.muilt_icon);
        if (list != null) {
            int size = list.size();
            if (size == 1) {
                icon.setVisibility(View.VISIBLE);
                String url = list.get(0).getSrc();
                ImageLoadUtil.load(getContext(),url,icon);
                muiltIcon.setVisibility(View.GONE);
            } else if (size > 1) {
                muiltIcon.setVisibility(View.VISIBLE);
                icon.setVisibility(View.GONE);
                for (int i = 0; i < size; i++) {
                    if (i < 3) {
                        ImageLoadUtil.load(getContext(),list.get(i).getSrc(),imgs[i]);
                    }
                }
            }else{
                icon.setVisibility(View.GONE);
                muiltIcon.setVisibility(View.GONE);
            }
        }else{
            icon.setVisibility(View.GONE);
            muiltIcon.setVisibility(View.GONE);
        }
        iconClose.setTag(position);
        iconClose.setOnClickListener(this);
    }

    private void setTextView(TextView textView, String text){
        if(text == null || text.trim().length() == 0){
            textView.setVisibility(View.GONE);
        } else{
            textView.setText(text);
            textView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 改变看过的内容颜色
     *
     * @param position
     */
    public void updateTitleColor(int position) {
        HeadLineData headLineData = (HeadLineData) getItem(position);
        headLineData.setLook(true);
        notifyDataSetChanged();
    }
}